package DataStructures.pokerhands;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;

import DataStructures.Card;

public class Flush extends Hand {
	
	private int[] sortedHandStrength;

	public Flush(Card[] cards) {
		super(cards);
	}

	@Override
	public int compareTo(Hand hand) {
		if(hand instanceof StraightFlush){
			return -1;
		} else if(hand instanceof FourOfAKind){
			return -1;
		} else if(hand instanceof FullHouse){
			return -1;
		} else if(hand instanceof Flush){
			int[] otherFlushValue = ((Flush) hand).getSortedHandStrength();
			//start from the highest card and work our way downwards.
			for(int cardpos = cards.length - 1; cardpos >= 0; cardpos--){
				if(sortedHandStrength[cardpos] > otherFlushValue[cardpos]) {
					return 1;
				} else if (sortedHandStrength[cardpos] < otherFlushValue[cardpos]){
					return -1;
				}
			}
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	protected void determineHandValue() throws IllegalArgumentException {
		sortedHandStrength = new int[cards.length];
		
		//only need to do one loop and check to see if all cards have same suit value.
		Card card = cards[0];
		sortedHandStrength[0] = card.getRank();
		for(int cardpos = 1; cardpos < cards.length; cardpos++){
			if(cards[cardpos].getSuit() != card.getSuit()){
				throw new IllegalArgumentException(cards.toString() + " not valid combination of cards for flush.");
			}
			sortedHandStrength[cardpos] = cards[cardpos].getRank();
			//TODO: check to see if this is wrongfully a straight flush and throw an exception.
		}
		
		Arrays.sort(sortedHandStrength);

	}

	public int[] getSortedHandStrength() {
		return sortedHandStrength;
	}

}
